const strTime = (format, formatObj) => format.replace(/{(y|m|d|h|i|s|w)+}/g, (result, key) => {
	let value = formatObj[key]
	if (result.length > 0 && value < 10) {
		value = '0' + value
	}
	return value
})
const formatDateYMDHM = (date) =>
	`${date.getFullYear()}-${(date.getMonth() + 1)<10?'0'+(date.getMonth() + 1):date.getMonth() + 1}-${(date.getDate())<10?'0'+(date.getDate()):date.getDate()} ${(date.getHours())<10?'0'+(date.getHours()):date.getHours()}:${(date.getMinutes())<10?'0'+(date.getMinutes()):date.getMinutes()}`;
const parseTimeAll = (time) => {
	const format = '{y}/{m}/{d} {h}:{i}:{s}'
	let date = null
	if (typeof time === 'string') {
		time = parseInt(time)
	}
	date = new Date(time * 1000)
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds()
	}

	return strTime(format, formatObj)
}
const parseTimeAllYMDHM = (time) => {
	const format = '{y}/{m}/{d} {h}:{i}'
	let date = null
	if (typeof time === 'string') {
		time = parseInt(time)
	}
	date = new Date(time * 1000)
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes()
	}

	return strTime(format, formatObj)
}
const parseTimeMD = (time) => {
	const format = '{m}月{d}日'
	const formatDate = '{y}年{m}月{d}日'
	let date1 = null
	if (typeof time === 'string') {
		time = parseInt(time)
	}
	date1 = new Date(time * 1000)
	const formatObj1 = {
		y: date1.getFullYear(),
		m: date1.getMonth() + 1,
		d: date1.getDate()
	}
	let date2 = new Date()
	const formatObj2 = {
		y: date2.getFullYear(),
		m: date2.getMonth() + 1,
		d: date2.getDate()
	}
	let compare1 = strTime(formatDate, formatObj1)
	let compare2 = strTime(formatDate, formatObj2)
	if (compare1 == compare2) {
		return '今天'
	} else {
		return strTime(format, formatObj1)
	}
}
/** 
 * file鏉炵悿ase64
 */
const blobToDataURL = (blob, cb) => {
	let reader = new FileReader();
	reader.onload = function(evt) {
		let base64 = evt.target.result;
		cb(base64);
	};
	reader.readAsDataURL(blob);
};
const hasNull = (params) => {
	var keys = Object.keys(params);
	for (var i = 0; i < keys.length; i++) {
		var obj = params[keys[i]];
		// console.log(keys[i],obj,obj == "")
		if (obj == null || obj === "" || obj == undefined) {
			return true;
		}
	}
	return false;
}

const filterRole = (value) => {
	let item = {}
	if (value == 1) {
		item = {
			text: '教师',
			bgc: '#CDD8FF',
			color: '#577BFD'
		}
	} else if (value == 2) {
		item = {
			text: '学生',
			bgc: '#C5E2FF',
			color: '#3397FA'
		}
	} else if (value == 3) {
		item = {
			text: '志愿者',
			bgc: '#FFC4B8',
			color: '#FC602F'
		}
	}
	return item
}

const filterMeetingStatus = (value) => {
	// 1未开始2进行中3结束4取消
	let text = ''
	if (value == 1) {
		text = '未开始'
	} else if (value == 2) {
		text = '进行中'
	} else if (value == 3) {
		text = '已结束'
	} else if (value == 4) {
		text = '已取消'
	}
	return text
}

function digitToChina(val) {
	if (val == '00') {
		return '周日'
	} else if (val == '01') {
		return '周一'
	} else if (val == '02') {
		return '周二'
	} else if (val == '03') {
		return '周三'
	} else if (val == '04') {
		return '周四'
	} else if (val == '05') {
		return '周五'
	} else if (val == '06') {
		return '周六'
	}
}
const parseToDateMD = (val) => {
	const format = '{m}月{d}日({w})'
	let date = new Date(val)
	const formatObj = {
		m: date.getMonth() + 1,
		d: date.getDate(),
		w: digitToChina(date.getDay())
	}

	return strTime(format, formatObj)
}
const parseToDateHM = (val) => {
	const format = '{h}:{i}'
	let date = new Date(val)
	const formatObj = {
		h: date.getHours(),
		i: date.getMinutes()
	}

	return strTime(format, formatObj)
}
const parseToDateAll = (val) => {
	const format = '{y}/{m}/{d} {h}:{i}:{s}'
	let date = new Date(val)
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds()
	}
	return strTime(format, formatObj)
}
const parseToDateYMD = (val) => {
	const format = '{y}/{m}/{d}'
	let date = new Date(val)
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
	}
	return strTime(format, formatObj)
}
const parseToDateYMD2 = (val) => {
	const format = '{y}-{m}-{d}'
	let date = new Date(val)
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
	}
	return strTime(format, formatObj)
}
// parseDateFormat('2021.8.31 08:12')
const parseDateFormat = (val) => {
	// 8:30 昨天 2021.12.12 08:12
	const target = new Date(val).getTime() / 1000
	const now = new Date().getTime() / 1000
	const oneDay = 86400
	const d = (now - target) / oneDay
	if (d < 1) {
		return `${new Date(val).getHours()}:${new Date(val).getMinutes()}`
	} else if (d > 1 && d < 2) {
		return `昨天 ${new Date(val).getHours()}:${new Date(val).getMinutes()}`
	} else {
		return `${new Date(val).getFullYear()}.${new Date(val).getMonth()+1}.${new Date(val).getDate()}`
	}
}

const Stickdatas = (times) => {
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var diffValue = now - new Date(times).getTime();
	if (diffValue < 0) {
		return;
	}
	var monthC = diffValue / month;
	var weekC = diffValue / (7 * day);
	var dayC = diffValue / day;
	var hourC = diffValue / hour;
	var minC = diffValue / minute;
	let result = ''
	if (monthC >= 1) {
		result = "" + parseInt(monthC) + "月前";
	} else if (weekC >= 1) {
		result = "" + parseInt(weekC) + "周前";
	} else if (dayC == 1) {
		result = "" + parseInt(dayC) + "昨天";
	} else if (dayC > 1) {
		result = "" + parseInt(dayC) + "天前";
	} else if (hourC >= 1) {
		result = "" + parseInt(hourC) + "小时前";
	} else if (minC >= 2) {
		result = "" + parseInt(minC) + "分钟前";
	} else result = "1分钟内";
	return result;
}

const parseParam = (obj) => {
	if(obj!={}){
		let result = '';
		let item;
		for (item in obj) {
			if (obj[item] && String(obj[item])) {
			  result += `&${item}=${obj[item]}`;
			}
		}
		if (result) {
			result = '?' + result.slice(1);
		}
		return result;
	}else{
		return ''
	}
};

const getCode = () => {
	return 200
}

const calculateAge = (cardNo) => {
	let birthYear = cardNo.substr(6,4)
	let nowYear = new Date().getFullYear()
	return nowYear - birthYear
}

export default {
	calculateAge,
	getCode,
	parseParam,
	Stickdatas,
	parseDateFormat,
	formatDateYMDHM,
	blobToDataURL,
	hasNull,
	parseTimeMD,
	parseTimeAll,
	parseTimeAllYMDHM,
	parseToDateMD,
	parseToDateHM,
	parseToDateAll,
	parseToDateYMD,
	parseToDateYMD2
}
